package com.guanyu.u2cache.model;


import lombok.*;
import org.springframework.http.HttpStatus;

/**
 * 返回结果
 *
 * @author zyj
 * Date 2021-06-01
 */
@Data
@EqualsAndHashCode()
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    /**
     * 业务状态码
     */
  //  @ApiModelProperty(value = "业务状态码")
    private Integer code;

    /**
     * 消息
     */
 //   @ApiModelProperty(value = "消息信息")
    private String message;

    /**
     * 数据
     */
   // @ApiModelProperty(value = "数据")
    private T data;

    public static Result success() {
        return Result.builder().code(HttpStatus.OK.value()).message("").data(null).build();
    }

    public static <T> Result<T> success(T data) {
        return getResult(HttpStatus.OK.value(), data);
    }

    private static <T> Result<T> getResult(Integer code, T data) {
        return getResult(code, data, "");
    }

    public static <T> Result<T> getResult(Integer code, T data, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static Result<Object> fail(HttpStatus httpStatus) {
        return Result.builder().code(httpStatus.value()).message(httpStatus.getReasonPhrase()).build();
    }
}
